// pages/start/start.js
var user = require('../../../utils/user.js');
var api = require('../../../config/api.js');
const util = require('../../../utils/util.js');

//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    user.checkLogin().then(res => {
      console.log("check login success");
      app.globalData.hasLogin = true;
      that.updateGloableData();
    }).catch(() => {
      //skip account auth
      console.log("check login false");
      app.globalData.hasLogin = false;
      that.updateGloableData();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  goLogin: function () {
    if (!this.data.hasLogin) {
      wx.redirectTo({
        url: "/pages/common/auth/login/login"
      });
    }
  },

  goHome: function(){
    wx.switchTab({
      url: '/pages/patient/home/home',
    })
  },

  goNurseHome: function () {
    if (!this.data.hasLogin) {
      wx.redirectTo({
        url: "/pages/nurse/nursehome/nursehome"
      });
    }
  },

  goRole: function () {
    if (!this.data.hasLogin) {
      wx.redirectTo({
        url: "/pages/common/role/role"
      });
    }
  },
  /*goNotVerified: function () {
      console.log("goNotVerified");
      wx.redirectTo({
        url: "/pages/common/notverified/notverified"
      });
  },*/
  getVerifyStatusfromServer: function(){
    let userInfo = wx.getStorageSync('userInfo');
    let that = this;
    console.log("getVerifyStatusfromServer with url: " + api.verifyStatus + ' userInfo.userid ' + userInfo.userid);
    util.request(api.verifyStatus,{
      userId: userInfo.userid
    }).then(function(res){
      console.log("getVerifyStatusfromServer" + JSON.stringify(res));
      var info_temp = res['data'];
      if(info_temp!=null && info_temp!=""){
        console.log("getVerifyStatusfromServer info_temp.status = " + info_temp.status);
        if(info_temp.status == true) {
          app.globalData.status = true;
        }
      }
    });
  },
  //更新用户信息
  updateGloableData: function(){
    //this.getVerifyStatusfromServer();

    let userInfo = wx.getStorageSync('userInfo');

    console.log("guomateng start page onLoad login userInfo = " + userInfo + " role" + userInfo.role 
      + " userid " + userInfo.userid + "userInfo.status " + userInfo.status);
    app.globalData.role = userInfo.role;
    app.globalData.userId = userInfo.userid;

    console.log("app.globalData.status = " + app.globalData.status);
    if(userInfo.avatarUrl){
      app.globalData.avatarUrl = userInfo.avatarUrl;
    }
    
    
    console.log("guomateng start page onLoad login = " + app.globalData.hasLogin 
        + " app.globalData.role " + app.globalData.role);

    if (app.globalData.hasLogin) {
          if(app.globalData.role == "undefine"
              || app.globalData.role == ""
              || app.globalData.role == null
              || app.globalData.role == 'none') {
            this.goRole();
          } else if(app.globalData.role == "nurse"){
            //if(app.globalData.status == false) {
            //    this.goNotVerified();
            //} else {
                console.log("guomateng go nurse = " + app.globalData.hasLogin);
                app.globalData.metaId = userInfo.metaId;
                console.log("guomateng go nurse app.globalData.metaId = " + app.globalData.metaId + " userInfo.metaId = " + userInfo.metaId);
                this.goNurseHome();
            //}
          }else {
            //if(app.globalData.status  == false) {
            //  this.goNotVerified();
            //} else {
              console.log("guomateng go home = " + app.globalData.hasLogin);
              app.globalData.metaId = userInfo.metaId;
              this.goHome();
            //}
          }
      //}
    }else{
      console.log('hehehe')
      // this.goLogin();
      this.goHome()
    }
  }
})